﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FantastPlatform.Framework.Service;
using FantastPlatform.Framework;
using NHibernate;
using NHibernate.Criterion;
using FantastPlatform.Decision.IModel;
using FantastPlatform.Decision.Model;
using FantastPlatform.Decision.IService;
using FantastPlatform.Framework.Common;

namespace FantastPlatform.Decision.Service
{
    public class PatrolTaskService : DataService<IPatrolTask, PatrolTask>, IPatrolTaskService
    {
        public IList<IPatrolTask> SearchPointTaskByUnitNameAndTime(int unitId, DateTime? Time, int PageSize, int SelfPage, out int TotalCount)
        {
            ICriteria criteria = Session.CreateCriteria(typeof(IPatrolTask));
            criteria.CreateAlias("Parent", "p").Add(Expression.Eq("Enable", true));
            //if (Time != null)
            //{
            //    criteria.Add(Expression.Gt("p.BeginTime", Time.Value.Date))
            //        .Add(Expression.Lt("p.BeginTime", Time.Value.Date.AddDays(1)));
            //}
            //else
            //{
            //    criteria.Add(Expression.Gt("p.BeginTime", DateTime.Now.Date));
            //}
            criteria.CreateAlias("p.ExecOrganization", "org")
                .Add(Expression.Eq("org.Id", unitId));

            ICriteria clone = criteria.Clone() as ICriteria;

            criteria.AddOrder(Order.Asc("p.BeginTime"));

            IList<IPatrolTask> list = criteria.SetFirstResult((SelfPage - 1) * PageSize).SetMaxResults(PageSize).List<IPatrolTask>();

            TotalCount = int.Parse(clone.SetProjection(Projections.RowCount()).UniqueResult().ToString());

            return list;
        }

        #region IPatrolTaskService 成员


        public void CreatePatrolTask(IPatrolTask patrolTask)
        {
            ITransaction transaction = null;
            ExecuteTransaction((ITransaction tran) =>
            {
                object Id = 0;
                IList<IPatrolTask> orgPatrolTaskList = patrolTask.Children;
                patrolTask.Children = null;
                //保存调度指令
                if (patrolTask.Id == 0)
                {
                    patrolTask.Leaf = false;
                    patrolTask.Level = 1;
                    DefaultValue(patrolTask);
                    Id = Session.Save(patrolTask);
                }
                else
                {
                    IPatrolTask oldPatrolTask = Session.Get<PatrolTask>(patrolTask.Id);
                    Check.Require<InvalidOperationException>(oldPatrolTask.CreateDate>DateTime.Now, "该排调警指令已结束，不能对历史数据进行操作");       
                    oldPatrolTask.Name = patrolTask.Name;
                    oldPatrolTask.Description = patrolTask.Description;
                    oldPatrolTask.DefaultLeader = patrolTask.DefaultLeader;
                    oldPatrolTask.Require = patrolTask.Require;
                    oldPatrolTask.BeginTime = patrolTask.BeginTime;
                    oldPatrolTask.EndTime = patrolTask.EndTime;
                    oldPatrolTask.DutyOrganization = patrolTask.DutyOrganization;
                    oldPatrolTask.ModifyDate = DateTime.Now;
                    oldPatrolTask.Enable = true;
                    Session.Update(oldPatrolTask);
                }
                //保存调度指令下的部门
                foreach (IPatrolTask item in orgPatrolTaskList)
                {
                    item.Parent = patrolTask;
                    if (item.Id == 0)
                    {
                        item.Leaf = false;
                        item.Level = 2;
                        DefaultValue(item);
                        Id = Session.Save(item);
                    }
                    else
                    {
                        IPatrolTask oldItem = Session.Get<PatrolTask>(item.Id);
                        oldItem.Name = item.Name;
                        oldItem.Description = item.Description;
                        oldItem.DutyOrganization = item.DutyOrganization;
                        oldItem.ExecOrganization = item.ExecOrganization;
                        oldItem.TaskType = item.TaskType;
                        oldItem.DefaultLeader = item.DefaultLeader;
                        oldItem.BeginTime = item.BeginTime == null ? oldItem.BeginTime : item.BeginTime;
                        oldItem.EndTime = item.EndTime == null ? oldItem.EndTime : item.EndTime;
                        oldItem.Require = item.Require;
                        oldItem.ModifyDate = DateTime.Now;
                        oldItem.Enable = true;
                        Session.Update(oldItem);
                    }
                    //保存调度部门下的点位
                    foreach (IPatrolTask point in item.Children)
                    {
                        point.Parent = item;
                        if (point.Id == 0)
                        {
                            point.Leaf = true;
                            point.Level = 3;
                            DefaultValue(point);
                            Id = Session.Save(point);
                        }
                        else
                        {
                            IPatrolTask oldPoint = Session.Get<PatrolTask>(point.Id);
                            oldPoint.Name = point.Name;
                            oldPoint.SelfOrgPersonNum = point.SelfOrgPersonNum;
                            oldPoint.OtherOrgPerson = point.OtherOrgPerson;
                            oldPoint.SelfOrgCar = point.SelfOrgCar;
                            oldPoint.OtherOrgName = point.OtherOrgName;
                            oldPoint.ModifyDate = DateTime.Now;
                            oldPoint.Enable = true;
                            Session.Update(oldPoint);
                        }
                    }
                }


            }, transaction);
        }

        #endregion
    }
}
